package com.nowcoder.community.repository;

import com.nowcoder.community.entity.LoginTicket;
import org.apache.ibatis.annotations.*;

@Mapper
@Deprecated // UserService 已用 redis 重构优化
public interface LoginTicketMapper {
    // 多个字符串会自动拼接，养成在字符串后面追加空格的习惯
    // Options 用来声明回填自增 id
    @Insert({
            "insert into login_ticket (user_id, ticket, status, expired) ",
            "values (#{userId}, #{ticket}, #{status}, #{expired})"
    })
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertTicket(LoginTicket loginTicket);

    @Select({
            "select id, user_id, ticket, status, expired ",
            "from login_ticket ",
            "where ticket = #{ticket}"
    })
    LoginTicket selectTicketByTicket(String ticket);

    // 动态拼接时需要加上 script 标签
    @Update({
            "<script>",
            "update login_ticket set status = #{status} where ticket = #{ticket} ",
            "<if test=\" ticket != null \">",
            "and 1 = 1",
            "</if>",
            "</script>"
    })
    int updateTicketStatus(String ticket, int status);
}
